package com.txm.drawPic;
/*
 * 画板
 *
 * */

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener, Runnable {

    boolean gameOver = false;
    UsTank usTank = null;

    //  敌方坦克
    static final Vector<EnemyTank> enemyTank = new Vector<>();
    //  子弹
    static final Vector<Bomb> Bombs = new Vector<>();

    //  定义坦克的数量
    static final int enemyNums = 3;
    //  记录障碍物
    static final Vector<Tank> obstacle = new Vector<>();

    Record record = null;

    Image img1 = null;
    Image img2 = null;
    Image img3 = null;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, Tank.PANEL_W, Tank.PANEL_H);

        if (usTank != null) {
//      画出我方坦克
            if (usTank.isLive()) {
                drawTank(g, usTank);
//      画子弹
                Vector<Bullet> bullets = usTank.getBullet();
                Iterator<Bullet> bullets_iterator = bullets.iterator();
                while (bullets_iterator.hasNext()) {
                    Bullet bullet = bullets_iterator.next();
                    if (bullet.isLive()) {
                        g.setColor(usTank.getColor());
                        g.draw3DRect(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight(), false);
                    } else {
                        bullets_iterator.remove();
                    }
                }
            } else {
//                Bombs.add(new Bomb(usTank.getX(), usTank.getY()));
                usTank.setLive(false);
                usTank = null;
            }
        }
//      画出敌方坦克
        Iterator<EnemyTank> iterator = enemyTank.iterator();
        while (iterator.hasNext()) {
            EnemyTank item = iterator.next();
            if (item.isLive()) {
                drawTank(g, item);
            } else {
                record.kill();
                obstacle.remove(item);
                iterator.remove();
            }
            Iterator<Bullet> EnemyTank_Bullets = item.getBullet().iterator();
            while (EnemyTank_Bullets.hasNext()) {
                Bullet bullet = EnemyTank_Bullets.next();
                if (bullet.isLive()) {
                    g.setColor(item.getColor());
                    g.draw3DRect(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight(), false);
                } else {
                    EnemyTank_Bullets.remove();
                }
            }
        }
//      画爆炸效果
        Iterator<Bomb> Bomb_iterator = Bombs.iterator();
        while (Bomb_iterator.hasNext()) {
            Bomb bomb = Bomb_iterator.next();
//            System.out.println(bomb);
            if (bomb.life > 6) {
                g.drawImage(img1, bomb.getX(), bomb.getY(), 60, 60, this);
            } else if (bomb.life > 3) {
                g.drawImage(img2, bomb.getX(), bomb.getY(), 60, 60, this);
            } else {
                g.drawImage(img3, bomb.getX(), bomb.getY(), 60, 60, this);
            }
            bomb.lifeDown();
            if (bomb.life == 0) {
                Bomb_iterator.remove();
            }
        }
//      游戏当前的信息
        drawRecord(g);
    }

    public void drawRecord(Graphics g) {
        int x = Tank.PANEL_W;
        int y = 0;
        int width = 300;
        int height = Tank.PANEL_H;

        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(x, y, width, height);
        g.setColor(Color.BLACK);
        Font font = new Font("宋体", Font.BOLD, 25);
        g.setFont(font);
        g.drawString("你累计击败的地方坦克", x + 20, y + 20);
        EnemyTank enemyTank1 = new EnemyTank("record", this);
        enemyTank1.setX(x + 20);
        enemyTank1.setY(y + 50);
        enemyTank1.setDirect("1");
        drawTank(g, enemyTank1);
        g.setColor(Color.BLACK);
        int killNum = record.getKillNum();
        g.drawString(killNum + "", x + enemyTank1.getMain_width() + enemyTank1.getTrack_width() * 2 + 40, y + 70);
    }

    public MyPanel() {
        record = new Record();
//        Bombs.add(new Bomb(0, 0));
        img1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/imgs/boom1.png"));
        img2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/imgs/boom2.png"));
        img3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/imgs/boom3.png"));
        usTank = new UsTank("us", this);
        obstacle.add(usTank);
        usTank.setEnemyTank(enemyTank);
        if (record.isNewGame) {
            //        标记敌方坦克
            for (int i = 0; i < enemyNums; i++) {
                EnemyTank enemyTank1 = new EnemyTank("enemy-" + i, this);
                enemyTank1.setX(i * 150);
                enemyTank.add(enemyTank1);
                obstacle.add(enemyTank1);
                Thread thread_enemyTank = new Thread(enemyTank1);
                thread_enemyTank.start();
                enemyTank1.setEnemyTank(usTank);
            }
        } else {
            for (int i = 0; i < record.tankPointInfo.size(); i++) {
                String[] point = record.tankPointInfo.get(i).split(" ");
                EnemyTank enemyTank1 = new EnemyTank("enemy-" + i, this);
                enemyTank1.setX(Integer.parseInt(point[0]));
                enemyTank1.setY(Integer.parseInt(point[1]));
                enemyTank1.setDirect(point[2]);
                enemyTank.add(enemyTank1);
                obstacle.add(enemyTank1);
                Thread thread_enemyTank = new Thread(enemyTank1);
                thread_enemyTank.start();
                enemyTank1.setEnemyTank(usTank);
            }
        }

    }

    //  * 东（East）* 南（South）* 西（West）* 北（North）
    public void drawTank(Graphics g, Tank tank) {
        Color color = null, color_alpha = null;
        int x = 0, y = 0, track_w = 0, track_h = 0, track_interval = 0, track_difference = 0;
        String direct = "";
        color = tank.getColor();
        color_alpha = tank.getColor_alpha();
        x = tank.getX();
        y = tank.getY();
        direct = tank.getDirect();
        track_w = tank.getTrack_width();
        track_h = tank.getTrack_height();
        track_interval = tank.getMain_width();
        track_difference = tank.getTrack_main_difference();

//        System.out.println(tank);
//
        if (direct != null && direct.equals("1")) {
            g.setColor(color);
//        左履带
            g.fill3DRect(x, y, track_w, track_h, false);
//        右履带
            g.fill3DRect(x + track_interval, y, track_w, track_h, false);
//        主体部分
            g.fill3DRect(x + track_w, y + track_difference, track_interval - track_w, track_h - 2 * track_difference, false);

//      画炮台
            g.setColor(color_alpha);
            int min = Math.min(track_interval - track_w, track_h - 2 * track_difference);
            g.fillOval(x + track_w - 1, y + track_difference + ((track_h - 2 * track_difference - min) / 2), min, min);
//      画炮管
            g.drawLine(x + track_w + min / 2 - 1, y, x + track_w + min / 2 - 1, y + track_h / 2);
//      设置发射子弹的位置
            tank.setBulletStratPosition(x + track_w + min / 2 - 1, y);
        }
        if (direct != null && direct.equals("3")) {
//            System.out.println("South");
            g.setColor(color);
//        左履带
            g.fill3DRect(x, y, track_w, track_h, false);
//        右履带
            g.fill3DRect(x + track_interval, y, track_w, track_h, false);
//        主体部分
            g.fill3DRect(x + track_w, y + track_difference, track_interval - track_w, track_h - 2 * track_difference, false);

//      画炮台
            g.setColor(color_alpha);
            int min = Math.min(track_interval - track_w, track_h - 2 * track_difference);
            int y1 = y + track_difference + ((track_h - 2 * track_difference - min) / 2);
            g.fillOval(x + track_w - 1, y1, min, min);
//      画炮管
            g.drawLine(x + track_w + min / 2 - 1, y + track_h / 2, x + track_w + min / 2 - 1, y + track_h);
//      设置发射子弹的位置
            tank.setBulletStratPosition(x + track_w + min / 2 - 1, y + track_h);
        }
        if (direct != null && direct.equals("2")) {
            g.setColor(color);

//        左履带
            g.fill3DRect(x, y, track_h, track_w, false);
//        右履带
            g.fill3DRect(x, y + track_interval, track_h, track_w, false);
//        主体部分
            g.fill3DRect(x + track_difference, y + track_w, track_h - 2 * track_difference, track_interval - track_w, false);

//      画炮台
            g.setColor(color_alpha);
            int min = (track_h - 2 * track_difference - track_interval + track_w) / 2;
//            System.out.println(min);
            g.fillOval(x + track_difference + min, y + track_w, track_interval - track_w, track_interval - track_w);
//      画炮管
            g.drawLine(x + track_difference + (track_h - 2 * track_difference) / 2, y + track_w + (track_interval - track_w) / 2, x + track_h, y + track_w + (track_interval - track_w) / 2);
//      设置发射子弹的位置
            tank.setBulletStratPosition(x + track_h, y + track_w + (track_interval - track_w) / 2);
        }
        if (direct != null && direct.equals("4")) {
            g.setColor(color);

//        左履带
            g.fill3DRect(x, y, track_h, track_w, false);
//        右履带
            g.fill3DRect(x, y + track_interval, track_h, track_w, false);
//        主体部分
            g.fill3DRect(x + track_difference, y + track_w, track_h - 2 * track_difference, track_interval - track_w, false);

//      画炮台
            g.setColor(color_alpha);
            int min = (track_h - 2 * track_difference - track_interval + track_w) / 2;
//            System.out.println(min);
            g.fillOval(x + track_difference + min, y + track_w, track_interval - track_w, track_interval - track_w);
//      画炮管
            g.drawLine(x + track_difference + (track_h - 2 * track_difference) / 2, y + track_w + (track_interval - track_w) / 2, x, y + track_w + (track_interval - track_w) / 2);

            //      设置发射子弹的位置
            tank.setBulletStratPosition(x, y + track_w + (track_interval - track_w) / 2);
        }
    }

    public void controlTank(Tank tank, KeyEvent e) {
        if (tank == null) return;
        if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == 'S') {
            tank.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == 'W') {
            tank.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == 'A') {
            tank.moveLeft();

        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == 'D') {
            tank.moveRight();
        }
        this.repaint();
    }

    public void shutBullet(Tank tank, KeyEvent e) {
        if (e.getKeyCode() == 'J') {
            tank.shut();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        controlTank(usTank, e);
        shutBullet(usTank, e);
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (usTank != null) {
            this.repaint();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        gameOver = true;
    }
}
